import pygame
class global_value():
    JUMP = False
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        #用于存储玩家行走动画
        player_walk_1 = pygame.transform.scale(pygame.image.load('game_custom/kunkun/ps1.png').convert_alpha(),(68,84))
        player_walk_2 = pygame.transform.scale(pygame.image.load('game_custom/kunkun/ps2.png').convert_alpha(),(68,84))
        player_walk_3 = pygame.transform.scale(pygame.image.load('game_custom/kunkun/ps3.png').convert_alpha(),(68,84))
        player_walk_4 = pygame.transform.scale(pygame.image.load('game_custom/kunkun/ps4.png').convert_alpha(),(68,84))
        player_walk_5 = pygame.transform.scale(pygame.image.load('game_custom/kunkun/ps5.png').convert_alpha(),(68,84))
        self.player_walk = [player_walk_1,player_walk_2,player_walk_3,player_walk_4,player_walk_5]
        #动画索引
        self.player_index = 0
        #跳跃动画
        self.player_jump = pygame.transform.scale(pygame.image.load('game_custom/kunkun/jump.png').convert_alpha(),(72,84))

        #用于给 draw 当参数
        #用于更新人物图片
        self.image = self.player_walk[self.player_index]
        #获取矩形 并将玩家置于地面
        self.rect = self.image.get_rect(midbottom = (10,812))

        #重力
        self.gravity = 0
        #播放跳跃音效
        self.jump_sound = pygame.mixer.Sound('game_custom/video/jump.mp3')
        self.jump_sound.set_volume(0.55)

    #判断是否跳跃
    def player_input(self):
        #限制只能跳一次
        if (global_value.JUMP is True) and self.rect.bottom >= 812:
            self.gravity = -20
            self.jump_sound.play()
    #重力
    def apply_gravity(self):
        self.gravity += 1
        self.rect.y += self.gravity
        #将玩家置于地面，防止掉入深渊
        if self.rect.bottom >= 812:
            self.rect.bottom = 812

    def animation_state(self):
        #跳跃动画
        if self.rect.bottom < 812:
            self.image = self.player_jump
        else:
            self.player_index += 0.1
            #重置动画索引
            if self.player_index >= len(self.player_walk):
                self.player_index = 0
            #走路动画
            self.image = self.player_walk[int(self.player_index)]
    
    def update(self):
        self.player_input()
        self.apply_gravity()
        self.animation_state()